package com.aijk.ehm.sys.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.sys.model.SysAdminRole;
import com.aijk.ehm.sys.model.SysAdminRoleExample;
import com.aijk.ehm.sys.model.qo.AdminRoleQo;
import com.aijk.ehm.sys.mapper.extra.SysAdminRoleExtMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service()
public class SysAdminRoleService {

    private static Logger logger = LoggerFactory.getLogger(SysAdminRoleService.class);

    @Autowired
    private SysAdminRoleExtMapper mapper;


    private SysAdminRoleExample getExample(AdminRoleQo qo) {
        SysAdminRoleExample example = new SysAdminRoleExample();
        SysAdminRoleExample.Criteria criteria = example.createCriteria();

        if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(Utils.toString(qo.getSortway()));
        if (qo != null ) {
            if (Utils.isNotEmpty(qo.getAdminId())) criteria.andAdminIdEqualTo(qo.getAdminId());
            if (Utils.isNotEmpty(qo.getRoleId())) criteria.andRoleIdEqualTo(qo.getRoleId());
            if (Utils.isNotEmpty(qo.getInAdminIds())) criteria.andAdminIdIn(qo.getInAdminIds());
            if (Utils.isNotEmpty(qo.getInRoleIds())) criteria.andRoleIdIn(qo.getInRoleIds());
        }

        return example;
    }

    public Integer count(AdminRoleQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public List<SysAdminRole> find(AdminRoleQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<SysAdminRole> pageList(PagingRequest pagingRequest, AdminRoleQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum(), pagingRequest.getPageSize());
        return new PageInfo<SysAdminRole>(mapper.selectByExample(getExample(qo)));
    }
    public SysAdminRole findOne(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    public Integer update(SysAdminRole record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    public Integer save(SysAdminRole record) {
        return mapper.insert(record);
    }

    public Integer delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }


    public void saveAdminRole(Integer adminId, String roleIds) {
        List<SysAdminRole> auths = new ArrayList<>();
        String[] rids = roleIds.split(",");
        for (String rid:rids){
            SysAdminRole auth =new SysAdminRole();
            auth.setAdminId(adminId);
            auth.setRoleId(Integer.parseInt(rid));
            auths.add(auth);
        }
        mapper.inserBatch(auths);
    }

    public int delAdminRole(Integer adminId) {
        AdminRoleQo qo=new AdminRoleQo();
        qo.setAdminId(adminId);
        return mapper.deleteByExample(getExample(qo));
    }
}
